<template>
	<div class="row">
		<div class="col-xs-12"  v-loading="loading2" element-loading-text="拼命加载中" style="width: 100%;">
			  <div class="block center">
				  	<span class="wrapper"  v-show="btnBtn">
				    <el-button :plain="true" type="danger" @click="triggerData">一键生成课程表</el-button>
				  </span>
			  </div>
			  <div class="block center" >
					  <span class="wrapper" v-show="AgingBtn">
					  	排课失败
    					<el-button :plain="true" type="danger" @click="reloadData">重新排课</el-button>
				  </span>
			  </div>

			  <div class="arranging-course-tips" v-show="ProgressBar">
		        <div class="progress">
		            <div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%">
		                <span class="sr-only">100% Complete</span>
		            </div>
		        </div>
		        <p>正在努力排课中,预计<span class="loading-date">3-10</span>分钟,请耐心等待哦</p>
		    </div>
			  <div id="page-container" v-show="errorPromptBigbox">
				  <div class="scheduling-error">
	            <i class="icon-cry-face"></i>
	            <p>排课失败!,请调整基础规则</p>
	            <span class="wrapper goDown">
						    <el-button :plain="true" type="danger" @click="reloadData">重新排课</el-button>
						  </span>
	            <!-- <el-row :gutter="30" class="errorFence">
							  <el-col :span="FenceNum" v-show="errorPromptLeftbox">
							  		<div class="grid-content bg-purple">
							  		  <div v-for="o in errorList1" :key="o" class="text item">{{o}}</div>
							  		</div>
							  </el-col>
							  <el-col :span="FenceNum" v-show="errorPromptRightbox">
							  		<div class="grid-content bg-purple">
							  		  <div v-for="o in errorList2" :key="o" class="text item">{{o}}</div>
							  		</div>
							  </el-col>
							</el-row> -->
	        </div>
			  </div>
		</div>
	</div>
</template>

<script>
	import utils from '@/assets/js/urlConfig.js'
	export default {
		data() {
			return{
				triggerList:[],
				errorList1:[],
				errorList2:[],
				loading2:false,
				errorPromptBigbox:false,
				errorPromptLeftbox:true,
				errorPromptRightbox:true,
				ProgressBar:false,
				btnBtn:true,
				AgingBtn:false,
				FenceNum:12,
				taskId:"",
				tnId:"",
			}
		},
		mounted() {
			this.getSessionData();
			this.Verification();
		},
		methods: {
			triggerData(){
				let _this = this;
				_this.ProgressBar = true;
				_this.AgingBtn = false;
				_this.errorPromptBigbox = false;
				_this.btnBtn = false;
				_this.$http.post(utils.UrlConfig.trigger+"&tnId="+this.tnId+"&taskId="+this.taskId)
				   .then(res => {
				     res = res.data
//				     console.log(res);
				     if (res.rtnCode === "0000000") {
				     		 _this.btnBtn = false;
				         _this.ProgressBar = true;
				     } else {
				         _this.$message.error(res.msg);
				     }
				 }).catch(error => {
				     console.log(error)
				 })
 				_this.judgeStateFun();
			},
			reloadData(){
				let _this = this;
				_this.ProgressBar = true;
				_this.AgingBtn = false;
				_this.errorPromptBigbox = false;
				_this.btnBtn = false;
				_this.$http.post(utils.UrlConfig.reload+"&tnId="+this.tnId+"&taskId="+this.taskId)
				   .then(res => {
				     res = res.data
//				     console.log(res);
				     if (res.rtnCode === "0000000") {
								 // 		 _this.btnBtn = false;
				        //  _this.ProgressBar = true;
				     } else {
				         _this.$message.error(res.msg);
				     }
				 }).catch(error => {
				     console.log(error)
				 })
 				this.judgeStateFun();
			},
			judgeStateFun(){
				let _this = this;
				setTimeout(function(){
					_this.$http.post(utils.UrlConfig.judgeState+"&tnId="+_this.tnId+"&taskId="+_this.taskId)
					   .then(res => {
					     res = res.data
					     if (res.rtnCode === "0000000") {
					         if(res.bizData == "1"){
										 	setTimeout(function(){
		         		   			_this.$router.push({path:'/automatic/MasterTimetable'});
											},10000)
					         }else if(res.bizData == "-1"){
					         	//查询失败原因
					         	_this.findError();
					         }else if(res.bizData == "0" || res.bizData == ""){
					         	//继续判断
					         	_this.judgeStateFun();
					         }
					     } else {
					         _this.$message.error(res.msg);
					     }
					 }).catch(error => {
					     console.log(error)
					 })
				},3000);
			},
			findError(){
				let _this = this;
				let error1 = "false";
				let error2 = "false";
				this.$http.post(utils.UrlConfig.findDesc+"&tnId="+this.tnId+"&taskId="+this.taskId)
				   .then(res => {
				     res = res.data
//				     console.log(res);
				     if (res.rtnCode === "0000000") {
				         error1 = "true";
				         _this.errorList1 = res.bizData;
 				         this.errorList1 = res.bizData;
				     } else {
				         _this.$message.error(res.msg);
				     }
				 }).catch(error => {
				     console.log(error)
				 });
				 this.$http.post(utils.UrlConfig.findRule+"&tnId="+this.tnId+"&taskId="+this.taskId)
				   .then(res => {
				     res = res.data
//				     console.log(res);
				     if (res.rtnCode === "0000000") {
				        error2 = "true";
			         	_this.errorList2 = res.bizData;
			         	this.errorList2 = res.bizData;
				     } else {
				         _this.$message.error(res.msg);
				     }
				 }).catch(error => {
				     console.log(error)
				 })
				 let judgeError = setInterval(function(){
				 		if(error1 == "true" && error2 == "true"){
				 			clearInterval(judgeError);
				 			if((_this.errorList1.length != 0 &&  _this.errorList2.length == 0 ) || (_this.errorList1.length == 0 &&  _this.errorList2.length != 0 )){
				 				_this.FenceNum = 24;
				 				if(_this.errorList1.length != 0){
				 					_this.errorPromptRightbox = false;
				 				}else{
									_this.errorPromptLeftbox = false;
				 				}
				 			}
				 			_this.errorPromptBigbox = true;
				 			_this.ProgressBar = false;
				 			_this.btnBtn = false;
				 		}
				 },1000);
			},
			getSessionData(){
				this.taskId = sessionStorage.getItem('taskId');
				this.tnId = sessionStorage.getItem('tnId');
				if(sessionStorage.getItem('canAgain') == "1"){
					this.reloadData();
				}
			},
			Verification(){
				if(sessionStorage.getItem('canAgain') == "1"){
					sessionStorage.removeItem('canAgain');
					return false;
				}
				if(sessionStorage.getItem('NameGo') == "NameGo"){
					let _this = this;
					this.$http.get(utils.UrlConfig.queryScheduleTaskStatus+"&tnId="+this.tnId+"&taskId="+this.taskId)
						 .then(res => {
							 res = res.data
							 if (res.rtnCode === "0000000") {
									_this.loading2 = false;
									 if(res.bizData == "4"){
											_this.$router.push({path:'/automatic/MasterTimetable'});
									 }else if(res.bizData == "3"){
											_this.btnBtn = false;
											_this.ProgressBar = true;
									 }else if(res.bizData == "2"){
											_this.btnBtn = false;
											_this.AgingBtn = true;
									 }
							 } else {
									 _this.loading2 = false;
									 _this.$message.error(res.msg);
							 }
					 }).catch(error => {
							 console.log(error)
					 })
				}
			}
		}
	}
</script>

<style>
	.center{
		text-align: center;
		margin-top: 50px;
	}
	.el-row {
    margin-bottom: 20px;
    &:last-child {
      margin-bottom: 0;
    }
  }
  .el-col {
    border-radius: 4px;
  }
  .bg-purple-dark {
    background: #99a9bf;
  }
  .bg-purple {
    background: #d3dce6;
  }
  .bg-purple-light {
    background: #e5e9f2;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
  .errorFence{
		margin: 0 auto !important;
		width: 600px;
  }
	.errorFence .text {
    font-size: 14px;
  }
  .errorFence .item {
    padding: 14px;
    text-align: left;
    color: #000;
  }
  .errorFence .clearfix:before,
  .errorFence .clearfix:after {
      display: table;
      content: "";
  }
  .errorFence .clearfix:after {
      clear: both
  }
  .errorFence .box-card {
    width: 480px;
  }
  .errorFence .errorTitle{
  		margin: 0 auto;
  		padding: 10px 0;
  }
  .goDown{
  		margin-bottom: 15px;
  		display: block;
  }
</style>
